var async = require('async');

var surveying = require('../controller/controller.surveying.js');

var roadshow = require('../controller/controller.roadshow.js');

var strategy = require('../controller/controller.strategy-meeting');

var user = require('../controller/controller.user');
var seller = require('../controller/controller.seller');
var industry = require('../controller/controller.industry');
var teamUser = require('../controller/controller.teamuser');
var team = require('../controller/controller.team');

var company = require('../controller/controller.dic-company');
var stockrecommend = require('../controller/controller.stockrecommend');
var teleConference = require('../controller/controller.teleconference.js');

var userjoin = require('../controller/controller.userjoin.js');
var rowshowlocation = require('../controller/controller.dic-roadshow-location.js');
var workdiary = require('../controller/controller.workdiary');
var util = require('../utils/util');
/**
 * 处理error
 * @param res
 * @param err
 */
function handleError(res, err) {
    console.log(err);
    res.redirect('/error');
}

/**
 * 获取调研列表
 * @param res
 * @param params
 * @param next
 */
function surveyList(res, params, next) {
    surveying.getList(params, function (err, result) {
        if (err) {
            handleError(res, err);
        } else {
            next(result);
        }
    });
}

/**
 * 获取路演列表
 * @param res
 * @param params
 * @param next
 */
function roadshowList(res, params, next) {
    roadshow.getList(params, function (err, result) {
        if (err) {
            handleError(res, err);
        } else {
            next(result);
        }
    });
}

/**
 * 获取策略会列表
 * @param res
 * @param params
 * @param next
 */
function strategyList(res, params, next) {
    strategy.getList(params, function (err, result) {
        if (err) {
            handleError(res, err);
        } else {
            next(result);
        }
    });
}

module.exports = {
    index: function (req, res, next) {
        user.getUserList({}, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next({users: result.obj});
            }
        })
    },

    list: function (req, res, next) {
        var params = req.query;
        var type = params.type;
        switch (type) {
            case "survey":
                surveyList(res, params, next);
                break;
            case "roadshow":
                roadshowList(res, params, next);
                break;
            case "strategy":
                strategyList(res, params, next);
                break;
            default:
                surveyList(res, params, next);
        }
    },

    surveyDetail: function (req, res, next) {
        var params = req.query;
        params.userId = req.session.uid;
        surveying.getInfo(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                var users = "";
                if (result && result.obj && result.obj.userjoinList != null) {
                    for (var i = 0; i < result.obj.userjoinList.length; i++) {
                        if (result.obj.userjoinList[i].userType == "participant") {
                            users += result.obj.userjoinList[i].userId.name + " ";
                        }
                    }
                }
                var isJoin = (result && result.obj && result.obj && result.obj.userjoinInfo) ? true : false;
                var isSelf = (req.session.uid == result.obj.createUid.rowId);

                next({
                    'info': result.obj,
                    moment: require('moment'),
                    'users': users,
                    'isJoin': isJoin,
                    'isSelf': isSelf
                });
            }
        })
    },

    initCreateSurvey: function (req, res, next) {
        async.series({
            seller: function (callback) {
                var params = req.query;
                var role = req.session.userinfo.role;
                if (role == '4' || role == '5') {
                    params.rowId = req.session.userinfo.seller.rowId;
                }

                seller.getEnabledSellerList(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            industry: function (callback) {
                industry.getIndustryList(null, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            person: function (callback) {
                user.getUserList({}, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            }
        }, function (err, results) {
            if (err) {
                handleError(res, err);
            } else {
                next(results);
            }
        })
    },

    createSurvey: function (req, res, next) {
        var params = {
            seller: req.body.seller,
            industry: req.body.industry,
            type: req.body.type_name,
            content: req.body.survey_desc,
            range: req.body.range_type,
            province: req.body.province,
            city: req.body.city,
            addr: req.body.address,
            contact: req.body.phone,
            contactName: req.body.contact,
            company: req.body.company_name,
            startTime: req.body.startTime,
            finishTime: req.body.finishTime,
            createUid: req.session.uid
        };

        var persons = req.body.person_name;


        surveying.create(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                if (persons instanceof Array) {
                    for (var i = 0; i < persons.length; i++) {
                        var params_join = {
                            actionId: result.obj.rowId,
                            userId: persons[i],
                            actionType: "surveying",
                            userType: "participant",
                            joinType: "onsite",
                            createUid: req.session.uid
                        };

                        userjoin.create(params_join, function (err, result_user) {
                            if (err) {
                                handleError(res, err);
                            }
                        })
                    }
                    next({info: result.obj, moment: require("moment")});
                } else {
                    if (persons != "") {
                        var params_join = {
                            actionId: result.obj.rowId,
                            userId: persons,
                            actionType: "surveying",
                            userType: "participant",
                            joinType: "onsite",
                            createUid: req.session.uid
                        };

                        userjoin.create(params_join, function (err, result_user) {
                            if (err) {
                                handleError(res, err);
                            } else {
                                next({info: result.obj, moment: require("moment")});
                            }
                        })
                    } else {
                        next({info: result.obj, moment: require("moment")});
                    }
                }

            }
        })
    },

    initEditSurvey: function (req, res, next) {
        async.series({
            seller: function (callback) {
                var params = req.query;
                var role = req.session.userinfo.role;
                var sellerParams={};
                if (role == '4' || role == '5') {
                    sellerParams.rowId = req.session.userinfo.seller.rowId;
                }
                seller.getEnabledSellerList(sellerParams, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            industry: function (callback) {
                industry.getIndustryList(null, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            info: function (callback) {
                var params = {'rowId': req.query.rowId};
                surveying.getInfo(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            person: function (callback) {
                user.getUserList({}, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            }
        }, function (err, results) {
            if (err) {
                handleError(res, err);
            } else {
                results.moment = require("moment");
                next(results);
            }
        })
    },

    editSurvey: function (req, res, next) {
        var params = {
            rowId: req.body.rowId,
            seller: req.body.seller,
            industry: req.body.industry,
            type: req.body.type_name,
            content: req.body.survey_desc,
            range: req.body.range_type,
            province: req.body.province,
            city: req.body.city,
            addr: req.body.address,
            contact: req.body.phone,
            contactName: req.body.contact,
            company: req.body.company_name,
            startTime: req.body.startTime,
            finishTime: req.body.finishTime
        };

        var persons = req.body.person_name;

        surveying.update(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                var params_delete = {
                    actionId: req.body.rowId
                };
                userjoin.deleUserJoinAll(params_delete, function (err, result_delete) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        if (persons instanceof Array) {
                            for (var i = 0; i < persons.length; i++) {
                                var params_join = {
                                    actionId: req.body.rowId,
                                    userId: persons[i],
                                    actionType: "surveying",
                                    userType: "participant",
                                    joinType: "onsite",
                                    createUid: req.session.uid
                                };

                                userjoin.create(params_join, function (err, result_user) {
                                    if (err) {
                                        handleError(res, err);
                                    }
                                })
                            }
                            next({info: result.obj, moment: require("moment")});
                        } else {
                            if (persons != "") {
                                var params_join = {
                                    actionId: req.body.rowId,
                                    userId: persons,
                                    actionType: "surveying",
                                    userType: "participant",
                                    joinType: "onsite",
                                    createUid: req.session.uid
                                };

                                userjoin.create(params_join, function (err, result_user) {
                                    if (err) {
                                        handleError(res, err);
                                    }
                                })

                                next({info: result.obj, moment: require("moment")});
                            } else {
                                next({info: result.obj, moment: require("moment")});
                            }
                        }
                    }
                })
            }
        })
    },

    surveyDelete: function (req, res, next) {
        var params = req.body;
        surveying.delete(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                var params_delete = {
                    actionId: req.body.rowId
                };
                userjoin.deleUserJoinAll(params_delete, function (err, result_delete) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        next(result);
                    }
                })
            }
        })
    },

    surveyJoin: function (req, res, next) {
        var params = req.body;
        params.userId = req.session.uid;
        userjoin.create(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result);
            }
        })
    },

    surveyExit: function (req, res, next) {
        var params = req.body;
        params.userId = req.session.uid;
        userjoin.deleUserJoin(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result);
            }
        })
    },

    roadshowDetail: function (req, res, next) {
        var params = req.query;
        params.userId = req.session.uid;
        roadshow.getInfo(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                var users = "";
                var analysts = "";
                if (result.obj.userjoinList != null) {
                    for (var i = 0; i < result.obj.userjoinList.length; i++) {
                        if (result.obj.userjoinList[i].userType == "participant") {
                            users += result.obj.userjoinList[i].userId.name + " ";
                        } else if (result.obj.userjoinList[i].userType == "analyst") {
                            analysts += result.obj.userjoinList[i].userId.name + " ";
                        }
                    }
                }
                var isJoin = result.obj.userjoinInfo ? true : false;
                var isSelf = (req.session.uid == result.obj.createUid.rowId);

                next({
                    'info': result.obj,
                    moment: require('moment'),
                    'users': users,
                    'analysts': analysts,
                    'isJoin': isJoin,
                    'isSelf': isSelf
                });
            }
        })
    },

    initCreateRoadshow: function (req, res, next) {
        async.series({
            seller: function (callback) {
                var params = req.query;
                var role = req.session.userinfo.role;
                if (role == '4' || role == '5') {
                    params.rowId = req.session.userinfo.seller.rowId;
                }
                seller.getEnabledSellerList(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            industry: function (callback) {
                industry.getIndustryList(null, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            person: function (callback) {
                user.getUserList({}, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            //这个获取会议室的接口暂时没有数据能够获取，待定
            meetingroom: function (callback) {
                rowshowlocation.getList(null, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            }
        }, function (err, results) {
            if (err) {
                handleError(res, err);
            } else {
                next(results);
            }
        })
    },

    createRoadshow: function (req, res, next) {
        var content;
        var persons;
        var analysts;
        var params_stockrecommends = new Array();
        var params_stockrecommend;

        if (req.body.type == '分析师') {
            content = req.body.content_analyst;
            persons = req.body.person_name_analyst;
            analysts = req.body.name_analyst;
        } else if (req.body.type == '上市公司') {
            content = "";
            persons = req.body.person_name_company;
        } else {
            content = req.body.content_expert;
            persons = req.body.person_name_expert;
        }

        var params = {
            startTime: req.body.startTime,
            finishTime: req.body.finishTime,
            seller: req.body.seller,
            industry: req.body.industry,
            address: req.body.meetingroom,
            type: req.body.type,
            recommend: req.body.recommend,
            content: content,
            subtypeCompany: req.body.type_money_company,
            guestCompany: req.body.visit_company,
            guestPro: req.body.visit_expert,
            fieldPro: req.body.expert_name_expert,
            companyName: req.body.company_name_company,

            createUid: req.session.uid
        };

        if (req.body.company_name_analyst_1 != "" && typeof(req.body.company_name_analyst_1) != "undefined") {
            params_stockrecommend = {
                stockId: req.body.company_name_analyst_1,
                trade: req.body.operate_analyst_1,
                createUid: req.session.uid
            };
            params_stockrecommends.push(params_stockrecommend);
        }

        if (req.body.company_name_analyst_2 != "" && typeof(req.body.company_name_analyst_2) != "undefined") {
            params_stockrecommend = {
                stockId: req.body.company_name_analyst_2,
                trade: req.body.operate_analyst_2,
                createUid: req.session.uid
            };
            params_stockrecommends.push(params_stockrecommend);
        }

        if (req.body.company_name_analyst_3 != "" && typeof(req.body.company_name_analyst_3) != "undefined") {
            params_stockrecommend = {
                stockId: req.body.company_name_analyst_3,
                trade: req.body.operate_analyst_3,
                createUid: req.session.uid
            };
            params_stockrecommends.push(params_stockrecommend);
        }

        if (req.body.recommend == "公司深度推荐") {
            params.analystType = "company";
            params.stockList = params_stockrecommends;
        } else {
            params.analystType = "";
        }

        roadshow.create(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                if (analysts instanceof Array) {
                    for (var i = 0; i < persons.length; i++) {
                        var params_join = {
                            actionId: result.obj.rowId,
                            userId: analysts[i],
                            actionType: "roadshow",
                            userType: "analyst",
                            joinType: "onsite",
                            createUid: req.session.uid
                        };

                        userjoin.create(params_join, function (err, result_user) {
                            if (err) {
                                handleError(res, err);
                            }
                        })
                    }
                } else if (typeof(analysts) != "undefined") {
                    if (persons != "") {
                        var params_join = {
                            actionId: result.obj.rowId,
                            userId: analysts,
                            actionType: "roadshow",
                            userType: "analyst",
                            joinType: "onsite",
                            createUid: req.session.uid
                        };

                        userjoin.create(params_join, function (err, result_user) {
                            if (err) {
                                handleError(res, err);
                            }
                        })
                    }
                }

                if (persons instanceof Array) {
                    for (var i = 0; i < persons.length; i++) {
                        var params_join = {
                            actionId: result.obj.rowId,
                            userId: persons[i],
                            actionType: "roadshow",
                            userType: "participant",
                            joinType: "onsite",
                            createUid: req.session.uid
                        };

                        userjoin.create(params_join, function (err, result_user) {
                            if (err) {
                                handleError(res, err);
                            }
                        })
                    }
                } else {
                    if (persons != "") {
                        var params_join = {
                            actionId: result.obj.rowId,
                            userId: persons,
                            actionType: "roadshow",
                            userType: "participant",
                            joinType: "onsite",
                            createUid: req.session.uid
                        };

                        userjoin.create(params_join, function (err, result_user) {
                            if (err) {
                                handleError(res, err);
                            }
                        })
                    }
                }

                next({info: result.obj, moment: require("moment")});
            }
        })
    },

    initEditRoadshow: function (req, res, next) {
        async.series({
            seller: function (callback) {
                var params = req.query;
                var role = req.session.userinfo.role;
                var sellerParams={};
                if (role == '4' || role == '5') {
                    sellerParams.rowId = req.session.userinfo.seller.rowId;
                }
                seller.getEnabledSellerList(sellerParams, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            industry: function (callback) {
                industry.getIndustryList(null, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            info: function (callback) {
                var params = {'rowId': req.query.rowId};
                roadshow.getInfo(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            person: function (callback) {
                user.getUserList({}, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            meetingroom: function (callback) {
                rowshowlocation.getList(null, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            }
        }, function (err, results) {
            if (err) {
                handleError(res, err);
            } else {
                results.moment = require("moment");
                next(results);
            }
        })
    },

    //TODO roadshow的update需要完善增加recommend的功能
    editRoadshow: function (req, res, next) {
        var content;
        var persons;
        var analysts;
        var params_stockrecommends = new Array();
        var params_stockrecommend;

        if (req.body.type == '分析师') {
            content = req.body.content_analyst;
            persons = req.body.person_name_analyst;
            analysts = req.body.name_analyst;
        } else if (req.body.type == '上市公司') {
            content = "";
            persons = req.body.person_name_company;
        } else {
            content = req.body.content_expert;
            persons = req.body.person_name_expert;
        }

        var params = {
            rowId: req.body.rowId,
            startTime: req.body.startTime,
            finishTime: req.body.finishTime,
            seller: req.body.seller,
            industry: req.body.industry,
            address: req.body.meetingroom,
            type: req.body.type,
            recommend: req.body.recommend,
            content: content,
            subtypeCompany: req.body.type_money_company,
            guestCompany: req.body.visit_company,
            guestPro: req.body.visit_expert,
            fieldPro: req.body.expert_name_expert,
            companyName: req.body.company_name_company,

            createUid: req.session.uid
        };

        if (req.body.company_name_analyst_1 != "" && typeof(req.body.company_name_analyst_1) != "undefined") {
            params_stockrecommend = {
                stockId: req.body.company_name_analyst_1,
                trade: req.body.operate_analyst_1,
                createUid: req.session.uid
            };
            params_stockrecommends.push(params_stockrecommend);
        }

        if (req.body.company_name_analyst_2 != "" && typeof(req.body.company_name_analyst_2) != "undefined") {
            params_stockrecommend = {
                stockId: req.body.company_name_analyst_2,
                trade: req.body.operate_analyst_2,
                createUid: req.session.uid
            };
            params_stockrecommends.push(params_stockrecommend);
        }

        if (req.body.company_name_analyst_3 != "" && typeof(req.body.company_name_analyst_3) != "undefined") {
            params_stockrecommend = {
                stockId: req.body.company_name_analyst_3,
                trade: req.body.operate_analyst_3,
                createUid: req.session.uid
            };
            params_stockrecommends.push(params_stockrecommend);
        }

        if (req.body.recommend == "公司深度推荐") {
            params.analystType = "company";
            params.stockList = params_stockrecommends;
        } else {
            params.analystType = "";
        }

        var params_delete = {
            actionId: req.body.rowId
        };


        stockrecommend.deleteByActionId(params_delete, function (err, result_delete) {
            if (err) {
                handleError(res, err);
            } else {
                roadshow.update(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        userjoin.deleUserJoinAll(params_delete, function (err, result_delete) {
                            if (err) {
                                handleError(res, err);
                            } else {
                                if (analysts instanceof Array) {
                                    for (var i = 0; i < persons.length; i++) {
                                        var params_join = {
                                            actionId: req.body.rowId,
                                            userId: analysts[i],
                                            actionType: "roadshow",
                                            userType: "analyst",
                                            joinType: "onsite",
                                            createUid: req.session.uid
                                        };

                                        userjoin.create(params_join, function (err, result_user) {
                                            if (err) {
                                                handleError(res, err);
                                            }
                                        })
                                    }
                                } else if (typeof(analysts) != "undefined") {
                                    if (persons != "") {
                                        var params_join = {
                                            actionId: req.body.rowId,
                                            userId: analysts,
                                            actionType: "roadshow",
                                            userType: "analyst",
                                            joinType: "onsite",
                                            createUid: req.session.uid
                                        };

                                        userjoin.create(params_join, function (err, result_user) {
                                            if (err) {
                                                handleError(res, err);
                                            }
                                        })
                                    }
                                }

                                if (persons instanceof Array) {
                                    for (var i = 0; i < persons.length; i++) {
                                        var params_join = {
                                            actionId: req.body.rowId,
                                            userId: persons[i],
                                            actionType: "roadshow",
                                            userType: "participant",
                                            joinType: "onsite",
                                            createUid: req.session.uid
                                        };

                                        userjoin.create(params_join, function (err, result_user) {
                                            if (err) {
                                                handleError(res, err);
                                            }
                                        })
                                    }
                                } else {
                                    if (persons != "") {
                                        var params_join = {
                                            actionId: req.body.rowId,
                                            userId: persons,
                                            actionType: "roadshow",
                                            userType: "participant",
                                            joinType: "onsite",
                                            createUid: req.session.uid
                                        };

                                        userjoin.create(params_join, function (err, result_user) {
                                            if (err) {
                                                handleError(res, err);
                                            }
                                        })
                                    }
                                }
                            }
                        })
                        next({info: result.obj, moment: require("moment")});
                    }

                })
            }
        })
    },

    roadshowDelete: function (req, res, next) {
        var params = req.body;
        roadshow.delete(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                var params_delete = {
                    actionId: req.body.rowId
                };
                stockrecommend.deleteByActionId(params_delete, function (err, result_delete) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        userjoin.deleUserJoinAll(params_delete, function (err, result_delete) {
                            if (err) {
                                handleError(res, err);
                            } else {
                                next(result);
                            }
                        })
                    }
                })
            }
        })
    },

    roadshowJoin: function (req, res, next) {
        var params = req.body;
        params.userId = req.session.uid;
        userjoin.create(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result);
            }
        })
    },

    roadshowExit: function (req, res, next) {
        var params = req.body;
        params.userId = req.session.uid;
        userjoin.deleUserJoin(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result);
            }
        })
    },

    strategyDetail: function (req, res, next) {
        var params = req.query;
        params.userId = req.session.uid;
        strategy.getInfo(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                if (err) {
                    handleError(res, err);
                } else {
                    var users = "";
                    if (result.obj.userjoinList != null) {
                        for (var i = 0; i < result.obj.userjoinList.length; i++) {
                            if (result.obj.userjoinList[i].userType == "participant") {
                                users += result.obj.userjoinList[i].userId.name + " ";
                            }
                        }
                    }
                    var isJoin = result.obj.userjoinInfo ? true : false;
                    var isSelf = (req.session.uid == result.obj.createUid.rowId);

                    next({
                        'info': result.obj,
                        moment: require('moment'),
                        'users': users,
                        'isJoin': isJoin,
                        'isSelf': isSelf
                    });
                }
            }
        })
    },

    initCreateStrategy: function (req, res, next) {

        async.series({
            seller: function (callback) {
                var params = req.query;
                var role = req.session.userinfo.role;
                if (role == '4' || role == '5') {
                    params.rowId = req.session.userinfo.seller.rowId;
                }
                seller.getEnabledSellerList(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            industry: function (callback) {
                industry.getIndustryList(null, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            person: function (callback) {
                user.getUserList({}, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            }
        }, function (err, results) {
            if (err) {
                handleError(res, err);
            } else {
                next(results);
            }
        })
    },

    createStrategy: function (req, res, next) {
        var params = {
            seller: req.body.seller,
            industry: req.body.industry,
            content: req.body.content,
            range: req.body.range_type,
            province: req.body.province,
            city: req.body.city,
            addr: req.body.address,
            startTime: req.body.startTime,
            finishTime: req.body.finishTime,
            createUid: req.session.uid
        };

        var persons = req.body.person_name;

        strategy.create(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                if (persons instanceof Array) {
                    for (var i = 0; i < persons.length; i++) {
                        var params_join = {
                            actionId: result.obj.rowId,
                            userId: persons[i],
                            actionType: "strategy",
                            userType: "participant",
                            joinType: "onsite",
                            createUid: req.session.uid
                        };

                        userjoin.create(params_join, function (err, result_user) {
                            if (err) {
                                handleError(res, err);
                            }
                        })
                    }
                    next({info: result.obj, moment: require("moment")});
                } else {
                    if (persons != "") {
                        var params_join = {
                            actionId: result.obj.rowId,
                            userId: persons,
                            actionType: "strategy",
                            userType: "participant",
                            joinType: "onsite",
                            createUid: req.session.uid
                        };

                        userjoin.create(params_join, function (err, result_user) {
                            if (err) {
                                handleError(res, err);
                            } else {
                                next({info: result.obj, moment: require("moment")});
                            }
                        })
                    } else {
                        next({info: result.obj, moment: require("moment")});
                    }
                }

            }
        })
    },

    initEditStrategy: function (req, res, next) {
        async.series({
            seller: function (callback) {
                var params = req.query;
                var role = req.session.userinfo.role;
                var sellerParams={};
                if (role == '4' || role == '5') {
                    sellerParams.rowId = req.session.userinfo.seller.rowId;
                }
                seller.getEnabledSellerList(sellerParams, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            industry: function (callback) {
                industry.getIndustryList(null, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            userjoininfo: function (callback) {
                var params = {
                    'actionId': req.query.rowId
                };
                userjoin.getUserJoinListByActionId(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            info: function (callback) {
                var params = {'rowId': req.query.rowId};
                strategy.getInfo(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            person: function (callback) {
                user.getUserList({}, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            }
        }, function (err, results) {
            if (err) {
                handleError(res, err);
            } else {
                results.moment = require("moment");
                next(results);
            }
        })
    },

    editStrategy: function (req, res, next) {
        var params = {
            rowId: req.body.rowId,
            seller: req.body.seller,
            industry: req.body.industry,
            content: req.body.content,
            range: req.body.range_type,
            province: req.body.province,
            city: req.body.city,
            addr: req.body.address,
            startTime: req.body.startTime,
            finishTime: req.body.finishTime
        };

        var persons = req.body.person_name;

        strategy.update(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                var params_delete = {
                    actionId: req.body.rowId
                };
                userjoin.deleUserJoinAll(params_delete, function (err, result_delete) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        if (persons instanceof Array) {
                            for (var i = 0; i < persons.length; i++) {
                                var params_join = {
                                    actionId: req.body.rowId,
                                    userId: persons[i],
                                    actionType: "strategy",
                                    userType: "participant",
                                    joinType: "onsite",
                                    createUid: req.session.uid
                                };

                                userjoin.create(params_join, function (err, result_user) {
                                    if (err) {
                                        handleError(res, err);
                                    }
                                })
                            }
                            next({info: result.obj, moment: require("moment")});
                        } else {
                            if (persons != "") {
                                var params_join = {
                                    actionId: req.body.rowId,
                                    userId: persons,
                                    actionType: "strategy",
                                    userType: "participant",
                                    joinType: "onsite",
                                    createUid: req.session.uid
                                };

                                userjoin.create(params_join, function (err, result_user) {
                                    if (err) {
                                        handleError(res, err);
                                    }
                                })

                                next({info: result.obj, moment: require("moment")});
                            } else {
                                next({info: result.obj, moment: require("moment")});
                            }
                        }
                    }
                })
            }
        })
    },

    strategyDelete: function (req, res, next) {
        var params = req.body;
        strategy.delete(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                var params_delete = {
                    actionId: req.body.rowId
                };
                userjoin.deleUserJoinAll(params_delete, function (err, result_delete) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        next(result);
                    }
                })
            }
        })
    },

    strategyJoin: function (req, res, next) {
        var params = req.body;
        params.userId = req.session.uid;
        userjoin.create(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result);
            }
        })
    },

    strategyExit: function (req, res, next) {
        var params = req.body;
        params.userId = req.session.uid;
        userjoin.deleUserJoin(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result);
            }
        })
    },

    initCreateTeleconference: function (req, res, next) {
        async.series({
            seller: function (callback) {
                var params = req.query;
                var role = req.session.userinfo.role;
                if (role == '4' || role == '5') {
                    params.rowId = req.session.userinfo.seller.rowId;
                }
                seller.getEnabledSellerList(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            industry: function (callback) {
                industry.getIndustryList(null, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            person: function (callback) {
                user.getUserList({}, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            }
        }, function (err, results) {
            if (err) {
                handleError(res, err);
            } else {
                next(results);
            }
        })
    },

    createTeleconference: function (req, res, next) {
        var params = {
            startTime: req.body.startTime,
            finishTime: req.body.finishTime,
            seller: req.body.seller,
            industry: req.body.industry,
            type: req.body.type_name,
            content: req.body.teleconference_desc,
            range: req.body.range_type,
            contact: req.body.phone,
            password: req.body.pwd,
            company: req.body.company_name,


            createUid: req.session.uid
        };

        var persons = req.body.person_name;

        teleConference.create(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                if (persons instanceof Array) {
                    for (var i = 0; i < persons.length; i++) {
                        var params_join = {
                            actionId: result.obj.rowId,
                            userId: persons[i],
                            actionType: "teleconference",
                            userType: "participant",
                            joinType: "tele",
                            createUid: req.session.uid
                        };

                        userjoin.create(params_join, function (err, result_user) {
                            if (err) {
                                handleError(res, err);
                            }
                        })
                    }
                    next({info: result.obj, moment: require("moment")});
                } else {
                    if (persons != "") {
                        var params_join = {
                            actionId: result.obj.rowId,
                            userId: persons,
                            actionType: "teleconference",
                            userType: "participant",
                            joinType: "tele",
                            createUid: req.session.uid
                        };

                        userjoin.create(params_join, function (err, result_user) {
                            if (err) {
                                handleError(res, err);
                            } else {
                                next({info: result.obj, moment: require("moment")});
                            }
                        })
                    } else {
                        next({info: result.obj, moment: require("moment")});
                    }
                }
            }
        })
    },

    initEditTeleconference: function (req, res, next) {
        async.series({
            seller: function (callback) {
                var params = req.query;
                var role = req.session.userinfo.role;
                var sellerParams={};
                if (role == '4' || role == '5') {
                    sellerParams.rowId = req.session.userinfo.seller.rowId;
                }
                seller.getEnabledSellerList(sellerParams, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            industry: function (callback) {
                industry.getIndustryList(null, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            info: function (callback) {
                var params = {'rowId': req.query.rowId};
                teleConference.getInfo(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            person: function (callback) {
                user.getUserList({}, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            }
        }, function (err, results) {
            if (err) {
                handleError(res, err);
            } else {
                results.moment = require("moment");
                next(results);
            }
        })
    },

    editTeleconference: function (req, res, next) {
        var params = {
            rowId: req.body.rowId,
            startTime: req.body.startTime,
            finishTime: req.body.finishTime,
            seller: req.body.seller,
            industry: req.body.industry,
            type: req.body.type_name,
            content: req.body.teleconference_desc,
            range: req.body.range_type,
            contact: req.body.phone,
            password: req.body.pwd,
            company: req.body.company_name,


            createUid: req.session.uid
        };

        var persons = req.body.person_name;

        teleConference.update(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                var params_delete = {
                    actionId: req.body.rowId,
                    userId: req.session.uid
                };
                userjoin.deleUserJoinAll(params_delete, function (err, result_delete) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        if (persons instanceof Array) {
                            for (var i = 0; i < persons.length; i++) {
                                var params_join = {
                                    actionId: req.body.rowId,
                                    userId: persons[i],
                                    actionType: "teleconference",
                                    userType: "participant",
                                    joinType: "tele",
                                    createUid: req.session.uid
                                };

                                userjoin.create(params_join, function (err, result_user) {
                                    if (err) {
                                        handleError(res, err);
                                    }
                                })
                            }
                            next({info: result.obj, moment: require("moment")});
                        } else {
                            if (persons != "") {
                                var params_join = {
                                    actionId: req.body.rowId,
                                    userId: persons,
                                    actionType: "teleconference",
                                    userType: "participant",
                                    joinType: "tele",
                                    createUid: req.session.uid
                                };

                                userjoin.create(params_join, function (err, result_user) {
                                    if (err) {
                                        handleError(res, err);
                                    }
                                })

                                next({info: result.obj, moment: require("moment")});
                            } else {
                                next({info: result.obj, moment: require("moment")});
                            }
                        }
                    }
                })
            }
        })
    },

    teleconferenceDelete: function (req, res, next) {
        var params = req.body;
        teleConference.delete(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                var params_delete = {
                    actionId: req.body.rowId
                };
                userjoin.deleUserJoinAll(params_delete, function (err, result_delete) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        next(result);
                    }
                })
            }
        })
    },

    teleconferenceDetail: function (req, res, next) {
        var params = req.query;
        params.userId = req.session.uid;
        teleConference.getInfo(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                var users = "";
                if (result.obj.userjoinList != null) {
                    for (var i = 0; i < result.obj.userjoinList.length; i++) {
                        if (result.obj.userjoinList[i].userType == "participant") {
                            users += result.obj.userjoinList[i].userId.name + " ";
                        }
                    }
                }
                var isJoin = result.obj.userjoinInfo ? true : false;
                var isSelf = (req.session.uid == result.obj.createUid.rowId);

                next({
                    'info': result.obj,
                    moment: require('moment'),
                    'users': users,
                    'isJoin': isJoin,
                    'isSelf': isSelf
                });
            }
        })
    },

    teleconferenceJoin: function (req, res, next) {
        var params = req.body;
        params.userId = req.session.uid;
        userjoin.create(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result);
            }
        })
    },

    teleconferenceExit: function (req, res, next) {
        var params = req.body;
        params.userId = req.session.uid;
        userjoin.deleUserJoin(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result);
            }
        })
    },

    /**
     *  根据userId获取team列表
     * @param req
     * @param res
     * @param next
     */
    getTeamListByUid: function (req, res, next) {
        var params = req.query;
        // params.userId = req.session.uid;
        async.waterfall([

            function (callback) {

                teamUser.getListByUId(req.query, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        var teamUserList = result.obj;
                        if (teamUserList) {

                            var teamIdList = [];
                            for (var i = 0; i < teamUserList.length; i++) {

                                var teamId = teamUserList[i].teamId;

                                teamIdList.push(teamId);

                            }
                        }
                        callback(null, teamIdList);
                    }
                })
            },
            function (teamIdList, callback) {

                team.getListByRowIds(teamIdList, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        callback(null, result);
                    }
                })
            }
        ], function (err, result) {

            if (err) {
                throw err;
            }
            else {
                next(result);
            }
        });

    },

    /**
     *  创建用户新接口
     * @param req
     * @param res
     * @param next
     */
    createUser: function (req, res, next) {
        // var params = req.query;
        var params = req.body;
        params.CreateUid = req.session.uid;
        //  params.createUid= '398daa48-0dc6-4441-bead-3882d5076121';
        params.teamId = req.session.teamId;
        //  params.teamId = '2edb963c-f5dc-4cf3-bcf5-d1acec7a8562';
        async.waterfall([

            function (callback) {

                user.create(params, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        if (result.code != '200') {

                            next(result);
                            return;

                        }

                        callback(null, result.obj);
                    }
                })
            },
            function (user, callback) {

                params.userId = user.rowId;
                teamUser.create(params, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        user.teamId = params.teamId;
                        callback(null, user);
                    }
                })
            }
        ], function (err, result) {

            if (err) {
                throw err;
            }
            else {
                next(result);
            }
        });

    },

    /**
     *  根据teamId获取team下的用户列表
     * @param req
     * @param res
     * @param next
     */
    getUserListByTeamId: function (req, res, next) {
        // var params = req.query;
        var params = req.query;
        params.userId = req.session.uid;
        async.waterfall([

            function (callback) {

                teamUser.getListByTeamId(req.query, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        var teamUserList = result.obj;
                        if (teamUserList) {

                            var userIdList = [];
                            for (var i = 0; i < teamUserList.length; i++) {

                                var userId = teamUserList[i].userId;
                                console.log("userId" + userId);
                                userIdList.push(userId);

                            }
                        }
                        callback(null, userIdList);
                    }
                })
            },
            function (userIdList, callback) {

                user.getListByRowIds(userIdList, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        callback(null, result);
                    }
                })
            }
        ], function (err, result) {

            if (err) {
                throw err;
            }
            else {
                next(result);
            }
        });

    },

    /**
     *  获取卖方活动列表(路演、调研、策略会、电话会议)
     * @param req
     * @param res
     * @param next
     */
    getSellerActivity: function (req, res, next) {

        var role = req.session.userinfo.role;
        if (role == '4' || role == '5') {

            req.query.seller=req.session.userinfo.seller.rowId;
        }
       req.query.userId = req.session.uid;
        //req.query.userId = '412ae636-22c9-44f9-8b3e-31dbe4d97d3c';
        async.parallel({

            userjoinResult: function (callback) {

                userjoin.getUserJoinListByUserId(req.query, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        callback(null, result.obj);
                    }
                })
            },


            roadshowResult: function (callback) {
                //var param = {teamId: "2edb963c-f5dc-4cf3-bcf5-d1acec7a8562"};
                roadshow.getList(req.query, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        callback(null, result.obj);
                    }
                });
            },

            serveyingResult: function (callback) {
                surveying.getList(req.query, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        callback(null, result.obj);
                    }
                });

            },

            strategymeetingResult: function (callback) {

                strategy.getList(req.query, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        callback(null, result.obj);
                    }
                });
            },
            teleConferenceResult: function (callback) {
                // var param = {teamId: req.session.teamId};
                teleConference.getList(req.query, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        callback(null, result.obj);
                    }
                });
            },
            workDiaryResult: function (callback) {
                workdiary.list(req.query, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    else {
                        callback(null, result.obj);
                    }
                });
            }
        }, function (err, result) {

            if (err) {
                handleError(res, err);
            } else {
                var data = {};

                data.roadshowResult = util.getUserJoin(result.userjoinResult, result.roadshowResult, req.session.uid);
                data.serveyingResult = util.getUserJoin(result.userjoinResult, result.serveyingResult, req.session.uid);
                data.strategymeetingResult = util.getUserJoin(result.userjoinResult, result.strategymeetingResult, req.session.uid);
                data.teleConferenceResult = util.getUserJoin(result.userjoinResult, result.teleConferenceResult, req.session.uid);
                data.workDiaryResult = result.workDiaryResult;
                data.code = 200;
                data.msg = "获取列表成功";

                next(data);
            }

        });


    },
    /**
     *  获取用户日程列表
     * @param req
     * @param res
     * @param next
     */
    getSchedule: function (req, res, next) {

         req.query.userId = req.session.uid;

       // req.query.userId = '6e040a81-e6a3-4434-872f-35fc2d59784d';

        userjoin.getUserJoinListByUserId(req.query, function (err, result) {
            if (err) {
                throw err;
            }
            else {
                var actionIdArr=result.obj;
                var rowIdArr=[];
                if(actionIdArr){
                    for (var i = 0; i < actionIdArr.length; i++) {
                        rowIdArr.push(actionIdArr[i].actionId);
                    }
                }

                req.query.rowIdArr = rowIdArr;

                async.parallel({
                    roadshowResult: function (callback) {
                        roadshow.getListByIds(req.query, function (err, result) {
                            if (err) {
                                throw err;
                            }
                            else {
                                callback(null, result.obj);
                            }
                        });
                    },

                    serveyingResult: function (callback) {
                        surveying.getListByIds(req.query, function (err, result) {
                            if (err) {
                                throw err;
                            }
                            else {
                                callback(null, result.obj);
                            }
                        });

                    },

                    strategymeetingResult: function (callback) {

                        strategy.getListByIds(req.query, function (err, result) {
                            if (err) {
                                throw err;
                            }
                            else {
                                callback(null, result.obj);
                            }
                        });
                    },
                    teleConferenceResult: function (callback) {
                        // var param = {teamId: req.session.teamId};
                        teleConference.getListByIds(req.query, function (err, result) {
                            if (err) {
                                throw err;
                            }
                            else {
                                callback(null, result.obj);
                            }
                        });
                    },
                    workDiaryResult: function (callback) {
                        workdiary.list(req.query, function (err, result) {
                            if (err) {
                                throw err;
                            }
                            else {
                                callback(null, result.obj);
                            }
                        });
                    }
                }, function (err, result) {

                    if (err) {
                        handleError(res, err);
                    } else {

                        result.code = 200;
                        result.msg = "获取列表成功";

                        next(result);
                    }

                });
            }
        })
    },
    initWorkDiary:function(req, res, next){
        var params = req.query;
        params.createUid = req.session.uid;
        var pageType = params.pageType;
        var resultObj = {};
        if (pageType != 'edit' && pageType != 'detail'){
            resultObj.pageType = 'add';
            resultObj.workdiary = {};
            next(resultObj);
            return;
        }else{
            resultObj.pageType = pageType;
        }
        if (params.rowId){
            workdiary.find(params, function (err, result) {
                if (err) {
                    handleError(res, err);
                } else {
                    resultObj.workdiary = result.obj;
                    next(resultObj);
                }
            });
        }
    },
    createWorkDiary:function(req, res, next){
        var params = req.body;
        params.createUid = req.session.uid;
        workdiary.create(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result);
            }
        });
    },
    updateWorkDiary:function(req, res, next){
        var params = req.body;
        params.createUid = req.session.uid;
        workdiary.update(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result);
            }
        });
    },
    deleteWorkDiary:function(req, res, next){
        var params = req.body;
        params.createUid = req.session.uid;
        workdiary.delete(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result);
            }
        });
    }

};
